﻿<?php
define('AUTHOR', 'Silver');
define('DATABASE', 'tt');

$current_dir = dirname(__FILE__).'/';
$project_dir = cleanPath($current_dir.'../../tt/');
$install_dir = cleanPath($current_dir.'../../install/');
$package_dir = cleanPath($install_dir.'packages/');

$applications = array(
	'TicketTracking' => array(
		'type' => 'kernel'
		, 'description' => 'Application TicketTracking'
		, 'version' => 'auto'
		, 'install' => array(
			'setup_files' => cleanPath($project_dir.'install/setup_files/')
			, 'screens' => array(
				'requirement' => array(
					0 => array(
						'type' => 'engine'
						, 'name' => 'php'
						, 'minversion' => '5.0'
					)
					, 1 => array(
						'type' => 'sgdb'
						, 'name' => 'mysql'
						, 'minversion' => '4.0'
					)
					, 2 => array(
						'type' => 'phpext'
						, 'name' => 'zlib'
					)
					, 3 => array(
						'type' => 'phpext'
						, 'name' => 'curl'
					)
					, 4 => array(
						'type' => 'phpext'
						, 'name' => 'mhash'
					)
					, 5 => array(
						'type' => 'phpext'
						, 'name' => 'mcrypt'
					)
					, 6 => array(
						'type' => 'phpext'
						, 'name' => 'mysql'
					)
				)
				, 'licence' => $project_dir.'licence.txt'
				, 'connection' => array(
					0 => 'mysql'
					, 1 => 'root'
				)
				, 'createtable' => array(
					'tarpath' => 'TicketTracking.sql'
					, 'structure' => '*'
					, 'datas' => array(
						0 => 'ttprefix__framework_country'
					)
				)
			)
			, 'scripts' => array(
				0 => array(
					'event' => 'aftercopy'
					, 'type' => 'mkdir'
					, 'path' => '/cache/smarty/cache'
				)
				, 1 => array(
					'event' => 'aftercopy'
					, 'type' => 'mkdir'
					, 'path' => '/cache/smarty/template_c'
				)
				, 2 => array(
					'event' => 'aftercopy'
					, 'type' => 'mkfile'
					, 'path' => '/conf/bdd.conf.php5'
					, 'datas' => '<?php
$conf[\'bdd\'][\'host\'] = \'«conf_db_host»\';
$conf[\'bdd\'][\'login\'] = \'«conf_db_login»\';
$conf[\'bdd\'][\'password\'] = \'«conf_db_password»\';
$conf[\'bdd\'][\'database\'] = \'«conf_db_database»\';
$conf[\'bdd\'][\'prefix\'] = \'«conf_db_prefix»\';
?>'
				)
				, 3 => array(
					'event' => 'aftercopy'
					, 'type' => 'mkfile'
					, 'path' => '/conf/login.conf.php5'
					, 'datas' => '<?php
$conf[\'login\'][\'gmail\'][\'service\'] = \'xapi\';
$conf[\'login\'][\'gmail\'][\'source_company\'] = \''.AUTHOR.'\';
$conf[\'login\'][\'gmail\'][\'source_application\'] = \'#APPLICATION_NAME#\';
$conf[\'login\'][\'gmail\'][\'source_version\'] = \'#APPLICATION_VERSION#\';
?>'
				)
				, 4 => array(
					'event' => 'aftercopy'
					, 'type' => 'mkfile'
					, 'path' => '/conf/appli.conf.php5'
					, 'datas' => '<?php
$conf[\'appli\'][\'root\'][\'login\'] = \'«conf_root_login»\';
$conf[\'appli\'][\'root\'][\'password\'] = \'«conf_root_password_md5»\';
$conf[\'appli\'][\'packages\'][\'#APPLICATION_NAME#\'][\'host\'] = \'«INSTALL_PACKAGE_HOST»\';
$conf[\'appli\'][\'packages\'][\'#APPLICATION_NAME#\'][\'version\'] = \'#APPLICATION_VERSION#\';
$conf[\'appli\'][\'packages\'][\'#APPLICATION_NAME#\'][\'history\'][\'#APPLICATION_VERSION#\'] = date(\'Y-M-d H:i:s\');
?>'
				)
				, 5 => array(
					'event' => 'aftercopy'
					, 'type' => 'sql'
					, 'datas' => 'INSERT INTO «conf_db_prefix»user_auth SET ua_s_login=\'«conf_root_login»\', ua_s_password=\'«conf_root_password_md5»\', ua_d_created=NOW(), ua_d_updated=NOW()'
				)
			)
		)
		, 'update' => array(
			'patch_files' => 'install/patch_files/'
			, 'screens' => array(
				'createtable' => array(
					'tarpath' => 'TicketTracking.sql'
					, 'structure' => 'E:\www\dev\tt_work\TicketTracking_update.mcp'
				)
			)
		)
		, 'extractfile' => array(
			'tarpath' => 'extract'
			, 'serverpath' => ''
			, 'files' => array(
				0 => array('', 'includes.php5')
				, 1 => array('class/', '*')
				, 2 => array('lang/fr/', '*')
				, 3 => array('view/', 'index.php5')
				, 4 => array('view/action/', '*')
				, 5 => array('view/css/', '*')
				, 6 => array('view/js/', '*')
				, 7 => array('view/screen/', '*')
			)
		)
	)
	, 'LanguageGb' => array(
		'type' => 'lang'
		, 'description' => 'Language Pack : English'
		, 'version' => 'auto'
		, 'extractfile' => array(
			'tarpath' => 'extract'
			, 'serverpath' => ''
			, 'files' => array(
				0 => array('lang/gb/', '*')
			)
		)
	)
	, 'Extrat' => array(
		'type' => 'extrat'
		, 'description' => 'Images : Avatars and Flags'
		, 'version' => 'auto'
		, 'extractfile' => array(
			'tarpath' => 'extract'
			, 'serverpath' => ''
			, 'files' => array(
				0 => array('view/extrat/', '*')
			)
		)
	)
);

include_once($current_dir.'class_archive.php5');

if(file_exists($install_dir.'install_versions.php5'))
{
	include_once($install_dir.'install_versions.php5');
}
else
{
	$versions = array();
	$packages_info = array();
}

if(isset($versions['last_revision']))
{
	$revision_from = intval($versions['last_revision']);
}
else 
{
	$revision_from = 0;
}

if(!isset($revision_to))
{
	echo 'Search last revision'."\n";
	$revision_to = $revision_from;
	$continue = true;
	while($continue)
	{
		echo ' request '.++$revision_to."\n";
		$command = '"C:\Program Files\Zend\ZendStudio-5.5.0\bin\SourceControl\svn\svn.exe" log E:\www\dev\tt -v -r '.$revision_to;
		$datas = array();
		exec($command.' 2>&1', $datas);
		if(isset($datas[1]))
		{
			echo ' '.$datas[1]."\n";
			if(strpos($datas[1], 'svn: No such revision ')!==false || (count($datas)>1 && strpos($datas[1], 'r'.$revision_to.' | ')!==0))
			{
				$revision_to--;
				$continue = false;
			}
		}
		else
		{
			echo '-'."\n";
		}
	}
	if($revision_from>=$revision_to)
	{
		echo 'bad revision';
		die();
	}
}

echo 'Revision From : '.$revision_from."\n";
echo 'Revision To : '.$revision_to."\n";

$files_updated = array();
$files_deleted = array();

if($revision_from != 0)
{
	echo 'Search Diff'."\n";
	
	$files = array();
	$command = '"C:\Program Files\Zend\ZendStudio-5.5.0\bin\SourceControl\svn\svn.exe" diff E:\www\dev\tt -r '.$revision_from.':'.$revision_to.' --summarize';
	$datas = array();
	exec($command.' 2>&1', $datas);
	
	if(count($datas)==0)
	{
		echo 'No file updated'."\n";
		die();
	}
	foreach ($datas as $row)
	{
		$action = substr($row, 0, 1);
		$path = substr(str_replace('\\', '/', trim(substr($row, 1))), strlen($project_dir));
		if(strpos($path, '.svn')!==false) continue;
		
		if(($action=='A' || $action=='M') && $path!='')
		{
			echo ' added : '.$path."\n";
			$files_updated[] = $path;
		}
		elseif($action=='D' && $path!='')
		{
			echo ' deleted : '.$path."\n";
			$files_deleted[] = $path;
		}
	}
}
$packages_file = array();
foreach ($applications as $application_name => $application_data)
{
	echo '
--- Application '.$application_name.' ---
';
	$packages_info[$application_name] = array('type' => $application_data['type'], 'description' => $application_data['description']);
	if(!isset($versions[$application_name]))
	{
		$versions[$application_name] = array();
	}
	if($application_data['version']=='auto')
	{
		if(count($versions[$application_name])>0)
		{
			$tmp = explode('.', $versions[$application_name][count($versions[$application_name])-1]);
			$tmp[3]++;
			$version = implode('.', $tmp);
		}
		else
		{
			$version = '0.0.1.0';
		}
	}
	else
	{
		$tmp = explode('.', $application_data['version']);
		$check = array();
		for($i=0; $i<4; $i++)
		{
			$check[$i] = intval($tmp[$i]);
		}
		if(implode('.', $check) != $application_data['version'])
		{
			echo 'Format de version ivalide !'."\n";
			die();
		}
		if(compareVersions($application_data['version'], $versions[$application_name][count($versions[$application_name])-1])<=0)
		{
			$tmp = explode('.', $versions[$application_name][count($versions[$application_name])-1]);
			$tmp[3]++;
			echo 'Version ivalide'."\n";
			echo ' Je propose : '.implode('.', $tmp)."\n";
			echo ' Autres versions :'."\n";
			
			print_r($versions);
			die();
		}
		$version = $application_data['version'];
	}
	Echo 'Version '.$version."\n";
	$installed = true;
	foreach(array('update', 'install') as $mode)
	{
		if($mode == 'update' && count($versions[$application_name]) == 0)
		{
			continue;
		}
		echo 'Create package '.$mode."\n";

		$package_name = $application_name.'.'.str_replace('.', '_', $version).'.'.$mode.'.tgz';
		$package_path = $package_dir.$package_name;
		if(file_exists($package_path))
		{
			unlink($package_path);
		}
		$package_files_updated = array();
		$package_files_deleted = array();
		if($mode == 'update')
		{
			$last_version = $versions[$application_name][count($versions[$application_name])-1];
			foreach($application_data['extractfile']['files'] as $template)
			{
				if($template[1] == '*')
				{
					$len = strlen($template[0]);
					foreach($files_updated as $file)
					{
						if(substr($file, 0, $len) == $template[0])
						{
							$package_files_updated[] = $file;
						}
					}
					foreach($files_deleted as $file)
					{
						if(substr($file, 0, $len) == $template[0])
						{
							$package_files_deleted[] = $file;
						}
					}
				}
				else
				{
					$template_file = $template[0].$template[1];
					foreach($files_updated as $file)
					{
						if($file == $template_file)
						{
							$package_files_updated[] = $file;
						}
					}
					foreach($files_deleted as $file)
					{
						if($file == $template_file)
						{
							$package_files_deleted[] = $file;
						}
					}
				}
			}
			if(count($package_files_updated) == 0 && count($package_files_deleted) == 0)
			{
				echo 'No file updated for this application'."\n";
				$installed = false;
				break;
			}
		}
		else
		{
			$package_files_updated[] = '*';
		}

		$package_files_patch = array();
		if(isset($application_data[$mode]['patch_files']))
		{
			echo 'Recherche des patch'."\n";
			$template = $application_data[$mode]['patch_files'];
			if($mode == 'update')
			{
				$len = strlen($template);
				foreach($files_updated as $file)
				{
					if(substr($file, 0, $len) == $template)
					{
						$package_files_patch[] = $file;
					}
				}
			}
			else
			{
				$h = opendir($project_dir.$template);
				while($f = readdir($h))
				{
					if($f=='.' || $f=='..' || !is_file($project_dir.$template.$f))
					{
						continue;
					}
					$package_files_patch[] = $template.$f;
				}
			}
		}
		
		
		echo ' Build XML file'."\n";
		$xml = buildIndexXml($application_name, $application_data, $mode, $package_files_deleted, $package_files_patch);
		file_put_contents($package_dir.'index.xml', $xml);
		
		if(isset($application_data[$mode]['screens']['createtable']))
		{
			echo ' Build SQL file'."\n";
			$sql_file = $application_data[$mode]['screens']['createtable']['tarpath'];
			if(file_exists($package_dir.$sql_file))
			{
				unlink($package_dir.$sql_file);
			}
			if($application_data[$mode]['screens']['createtable']['structure'] == '*')
			{
				$link = @mysql_connect('localhost', 'root', '');
				mysql_select_db(DATABASE, $link);
				$sql = 'SHOW TABLES FROM '.DATABASE;
				$res_table = mysql_query($sql, $link);
				$all_sqls = array();
				
				while($row = mysql_fetch_row($res_table))
				{
					$table = $row[0];
					$sql = 'SHOW CREATE TABLE `'.$table.'`';
					$res_create = mysql_query($sql, $link);
					if($row = mysql_fetch_row($res_create))
					{
						$all_sqls[$table] = $row[1];
					}
					mysql_free_result($res_create);
				}
				
				mysql_free_result($res_table);
				mysql_close($link);


				$table_pos = array();
				$cpt = 0;
				foreach ($all_sqls as $table => $sql)
				{
					if($table == '#') continue;
					$table_pos[$table] = $cpt++;
				}
				$try = 0;
				$continue = true;
				while($continue && $try<100)
				{
					$try++;
					$continue = false;
					foreach ($all_sqls as $table => $sql)
					{
						if($table == '#') continue;
						
						preg_match_all('/REFERENCES `([^`]+)`/', $sql, $matches);
						foreach($matches[1] as $ref)
						{
							if($ref==$table) continue;
							if(!isset($table_pos[$ref])) continue;
							if($table_pos[$table]>$table_pos[$ref])continue;
							$table_pos[$table] = $table_pos[$ref]+1;
							$continue = true;
						}
					}
				}
				asort($table_pos);
				$sqls = array();
				if($mode=='install')
				{
					foreach($table_pos as $table => $index)
					{
						$sqls[] = 'DROP TABLE IF EXISTS `'.$table.'`'; 
					}
				}
					
				foreach($table_pos as $table => $index)
				{
					$sqls[] = $all_sqls[$table];
				}
					
				$sql = implode(";\n", $sqls);
				file_put_contents($package_dir.$sql_file, $sql);
			}
			else
			{
				$command = '"c:\Program Files\EMS\DB Comparer for MySQL\mycomparerc.exe" '.$application_data[$mode]['screens']['createtable']['structure'].' '.$package_dir.$sql_file;
				$datas = array();
				exec($command.' 2>&1', $datas);
				
				if(file_exists($package_dir.$sql_file))
				{
					$sqls = str_replace(array('`'.DATABASE.'.', '`'.DATABASE.'/'), array('`', '`'), file_get_contents($package_dir.$sql_file));
					$sqls = preg_split('/;[\r\n]+/', $sqls);
					
					$all_sqls = array();
					$all_sqls['#'] = array();
					foreach($sqls as $sql)
					{
						if($sql == '') continue;
						if(substr($sql, 0, 14) == 'CREATE TABLE `')
						{
							$pos = strpos($sql, '`', 14);
							$all_sqls[substr($sql, 14, $pos-14)] = $sql;					
						}
						else
						{
							$all_sqls['#'][] = $sql;	
						}
					}
					
					$table_pos = array();
					$cpt = 0;
					foreach ($all_sqls as $table => $sql)
					{
						if($table == '#') continue;
						$table_pos[$table] = $cpt++;
					}
					$try = 0;
					$continue = true;
					while($continue && $try<100)
					{
						$try++;
						$continue = false;
						foreach ($all_sqls as $table => $sql)
						{
							if($table == '#') continue;
							
							preg_match_all('/REFERENCES `([^`]+)`/', $sql, $matches);
							foreach($matches[1] as $ref)
							{
								if($ref==$table) continue;
								if(!isset($table_pos[$ref])) continue;
								if($table_pos[$table]>$table_pos[$ref])continue;
								$table_pos[$table] = $table_pos[$ref]+1;
								$continue = true;
							}
						}
					}
					asort($table_pos);
					$sqls = array();
					if($mode=='install')
					{
						foreach($table_pos as $table => $index)
						{
							$sqls[] = 'DROP TABLE IF EXISTS `'.$table.'`'; 
						}
					}
					
					foreach($table_pos as $table => $index)
					{
						$sqls[] = $all_sqls[$table];
					}
					foreach($all_sqls['#'] as $sql)
					{
						$sqls[] = $sql;
					}
					$sql = implode(";\n", $sqls);
					file_put_contents($package_dir.$sql_file, $sql);
				}
				else 
				{
					file_put_contents($package_dir.$sql_file, '');
				}
			}
			if(isset($application_data[$mode]['screens']['createtable']['datas']))
			{
				$sqls = '';
				foreach($application_data[$mode]['screens']['createtable']['datas'] as $table)
				{
					$link = mysql_connect('localhost', 'root', '');
					mysql_select_db(DATABASE, $link);
					
					$sql = 'SELECT * FROM `'.$table.'`';
					$res_data = mysql_query($sql, $link);
					$head = '';
					$body = array();
					while($row = mysql_fetch_assoc($res_data))
					{
						$fields = array_keys($row);
						if($head=='')
						{
							$head = 'INSERT INTO `'.$table.'`
(`'.implode('`, `', $fields).'`)
VALUES
';
						}
						$sql = '(';
						for($i=0; $i<count($fields); $i++)
						{
							$field = $fields[$i];
							$sql.='\''.addslashes($row[$field]).'\',';
						}
						$body[] = substr($sql, 0, -1).')';
					}
					mysql_free_result($res_data);
					for ($i=0; $i<count($body); $i+=50)
					{
						$sqls .= $head.implode(',
', array_slice($body, $i, 50)).';
';
					}
					$sqls .= '
';
				}
				file_put_contents($package_dir.$sql_file, file_get_contents($package_dir.$sql_file).";\n".$sqls);
			}
		}

		echo ' ZIP files'."\n";
		$packages_file[] = $package_name;
		$gzip = new gzip_file($package_name);
		$gzip->set_options(array('basedir' => $package_dir, 'level' => 9));
		if($mode == 'update')
		{
			foreach ($package_files_updated as $path)
			{
				$gzip->add_files($path, $project_dir.'/', 'extract/');
			}
		}
		else
		{
			foreach($application_data['extractfile']['files'] as $template)
			{
				$gzip->add_files($template[1], $project_dir.'/'.$template[0], 'extract/'.$template[0]);
			}
		}
		$gzip->add_files('index.xml', $package_dir, '');
		if(isset($application_data[$mode]['setup_files']))
		{
			$gzip->add_files('*', $application_data[$mode]['setup_files'], 'setup/');
		}
		if(isset($application_data[$mode]['screens']['createtable']))
		{
			$gzip->add_files($application_data[$mode]['screens']['createtable']['tarpath'], $package_dir, 'scripts/');
		}
		if(isset($application_data[$mode]['screens']['licence']) && file_exists($application_data[$mode]['screens']['licence']))
		{
			$gzip->add_files('licence.txt', dirname($application_data[$mode]['screens']['licence']), 'scripts/');
		}
		foreach ($package_files_patch as $file)
		{
			file_put_contents($project_dir.$file, '<?php
if(isset($_SESSION[\'install\'][\'patch_execute\']) && (time() - $_SESSION[\'install\'][\'patch_execute\'])<=2)
{
?>
'.file_get_contents($project_dir.$file).'
<?php
}
else
{
	echo \'updateDisplayError("«!ADMIN_NSTALL_ERROR_EXECUTINGSCRIPT»");\';
	$error = true;
}
?>');
			$gzip->add_files($file, $project_dir, 'scripts/');
		}
		
		
		$gzip->create_archive();

		echo ' unlink temporary files'."\n";
		unlink($package_dir.'index.xml');
		if(isset($application_data[$mode]['screens']['createtable']))
		{
			unlink($package_dir.$application_data[$mode]['screens']['createtable']['tarpath']);
		}
	}
	if($installed)
	{
		$versions[$application_name][] = $version;
	}

	echo '
----------------------------------
';
}

echo 'Connection au FTP'."\n";
$ftp_serveur = "ftpperso.free.fr";
$ftp_user_name = "j.derusse";
$ftp_user_pass = "ss53426";

$conn_id = ftp_connect($ftp_serveur);
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
if($login_result)
{
	foreach($packages_file as $package_name)
	{
		echo ' upload de '.$package_name."\n";
		if(!ftp_put($conn_id, 'tt/packages/'.$package_name, $package_dir.$package_name, FTP_BINARY))
		{
			echo ' ERROR !!'."\n";
			die();
		}
	}
	echo ' upload de versions.php5'."\n";

	$versions['last_revision'] = $revision_to;
file_put_contents($install_dir.'install_versions.php5', '<?php
$versions = '.var_export($versions, true).';
$packages_info = '.var_export($packages_info, true).';
?>');
	
	if(!ftp_put($conn_id, 'tt/install_versions.php5', $install_dir.'install_versions.php5', FTP_ASCII))
	{
		echo ' ERROR !!'."\n";
	}
}
@ftp_close($conn_id);


function buildIndexXml($application_name, $application_data, $mode, $package_files_deleted=array(), $package_files_patch=array())
{
	global $version, $last_version;
	$xml = '<?xml version=\'1.0\' standalone=\'yes\'?>
<ttsetup version="1.0" mode="'.$mode.'">
	<application>
		<name>'.$application_name.'</name>
		<version>'.$version.'</version>
		<author>'.AUTHOR.'</author>
		<startpath>./view/</startpath>
	</application>
	<screens css="tt_css.css">
';
	if($mode=='update')
	{
		$xml.='		<requirement type="package" name="'.$application_name.'" minversion="'.$last_version.'" />';
		
	}
	if(isset($application_data[$mode]['screens']))
	{
		if(isset($application_data[$mode]['screens']['requirement']))
		{
			foreach($application_data[$mode]['screens']['requirement'] as $requirement_data)
			{
				$xml.='		<requirement type="'.$requirement_data['type'].'" name="'.$requirement_data['name'].'"';
				if(isset($requirement_data['minversion']))
				{
					$xml.=' minversion="'.$requirement_data['minversion'].'"';
				}
				$xml.=' />
';
			}
		}
		if(isset($application_data[$mode]['screens']['licence']) && file_exists($application_data[$mode]['screens']['licence']))
		{
			$xml.='		<licence path="scripts/licence.txt" />
';
		}
		if(isset($application_data[$mode]['screens']['connection']))
		{
			foreach($application_data[$mode]['screens']['connection'] as $connection_data)
			{
				$xml.='		<connection type="'.$connection_data.'" />
';
			}
		}
		$xml.='		<extractfile tarpath="'.$application_data['extractfile']['tarpath'].'" serverpath="'.$application_data['extractfile']['serverpath'].'"/>
';
		if(isset($application_data[$mode]['screens']['createtable']))
		{
			$xml.='		<createtable path="scripts/'.$application_data[$mode]['screens']['createtable']['tarpath'].'" />
';
		}
	}
	$xml.='	</screens>
';
	if(isset($application_data[$mode]['scripts']))
	{
		$xml.='	<scripts>
';
		foreach($package_files_patch as $file)
		{
			$xml.='		<script event="aftercopy" type="patch" path="'.$file.'" />
';
		}
		foreach($application_data[$mode]['scripts'] as $script_data)
		{
			$xml.='		<script event="'.$script_data['event'].'" type="'.$script_data['type'].'"';
			if(isset($script_data['path']))
			{
				$xml.=' path="'.$script_data['path'].'"';
			}
			if(isset($script_data['datas']))
			{
				$xml.='><![CDATA['.str_replace(array('#APPLICATION_NAME#', '#APPLICATION_VERSION#'), array($application_name, $version), $script_data['datas']).']]></script>
';
			}
			else
			{
				$xml.=' />
';
			}
		}
		$xml.='	</scripts>
';
	}
	if(count($package_files_deleted)>0)
	{
		$xml.='	<deleted>
';
		foreach ($package_files_deleted as $path)
		{
			$xml.='		<path>'.$path.'</path>
';
		}
		$xml.='	</deleted>
';
	}
	$xml.='</ttsetup>';
	return $xml;
}
function cleanPath($path)
{
	$path = ereg_replace("[\\\/]", "/", trim($path));
	$arr = explode("/", $path);
	$new_path  = array();
	foreach ($arr as $dir)
	{
		if($dir=="" || $dir==".")
		{
			if(count($new_path)>0)
			{
				continue;
			}
		}
		if($dir=="..")
		{
			if(count($new_path)>0)
			{
				array_pop($new_path);
			}
			else
			{
				$new_path[] = "..";
			}
		}
		else
		{
			$new_path[] = $dir;
		}
	}
	if(substr($path, strlen($path)-1)=="/")
	{
		$new_path[]="";
	}
	return implode("/", $new_path);
}

/**
 * Compare two version and return -1 if $v1 is lower than $v2, +1 if $v1 greater than $v2 and 0 if the two versions are equals
 *
 * @param string $v1
 * @param string $v2
 * 
 * @return int
 */
function compareVersions($v1, $v2)
{
	$v1 = explode('.', $v1);
	$v2 = explode('.', $v2);
	foreach ($v1 as $i => $v)
	{
		if(intval($v1[$i])<intval($v2[$i]))
			return -1;
		if(intval($v1[$i])>intval($v2[$i]))
			return 1;
	}
	return 0;
}

?>